package com.xin.libandroidpaysocial.share;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.xin.libandroidpaysocial.R;


/**
 * Created by xin on 2019/1/9.
 * 功能描述：分享
 */
public class ShareActivity extends AppCompatActivity {
    public static final String ExtraShareData = "ShareActivity_ExtraShareData";
    public static final String ExtraLayoutId = "ShareActivity_ExtraLayoutId";
    ShareData shareData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = getIntent().getIntExtra(ExtraLayoutId, R.layout.activity_share);
        setContentView(layoutId);
        initView();
    }

    public void setShareViews(View wxView, View wxTimeLine, View qqView) {
        if (wxView != null) {
            wxView.setOnClickListener(v -> onShareWxClicked());
        }
        if (wxTimeLine != null) {
            wxTimeLine.setOnClickListener(v -> onShareWxTimeClicked());
        }
        if (qqView != null) {
            qqView.setOnClickListener(v -> onShareQqClicked());
        }
    }


    public void setCloseViews(View... closeView) {
        if (closeView != null && closeView.length > 0) {
            for (View item : closeView) {
                item.setOnClickListener(v -> onCloseClicked());
            }
        }
    }

    private void initView() {
        ShareUtils.init(this);
        shareData = getIntent().getParcelableExtra(ExtraShareData);
        if (shareData == null) {
            finish();
            overridePendingTransition(0, 0);
        }
    }

    /**
     * 分享朋友圈
     */
    public void onShareWxTimeClicked() {
        ShareUtils.wx(this, shareData,
                SendMessageToWX.Req.WXSceneTimeline,
                new ShareCallBack2());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void onShareWxClicked() {
        ShareUtils.wx(this, shareData,
                SendMessageToWX.Req.WXSceneSession,
                new ShareCallBack2());
    }

    public void onShareQqClicked() {
        ShareUtils.qq(this, shareData, false, new ShareCallBack2());
    }

    public void onShareSinaClicked() {
    }

    public void onCloseClicked() {
        finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(0, 0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ShareUtils.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        ShareUtils.onNewIntent(intent);
    }

    /**
     * 分享回调事
     */
    private class ShareCallBack2 implements ShareUtils.ShareCallBack {
        @Override
        public void onComplete() {
            log("onComplete() called");
            toast("分享成功");
            finish();
        }

        @Override
        public void onCancel() {
            log("onCancel() called");
            toast("分享取消");
            finish();
        }

        @Override
        public void onError() {
            log("onError() called");
            toast("分享失败");
            finish();
        }
    }

    private void toast(String str) {

    }

    private void log(String s) {

    }
}
